package edu.gatech.cs2340.mule.view;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.gatech.cs2340.mule.model.Player;
import edu.gatech.cs2340.mule.presenter.MuleGamePresenter;


public class ProductionPanel extends JPanel {
	
	private JPanel p1Color;
	private JPanel p2Color;
	private JPanel p3Color;
	private JPanel p4Color;
	private JLabel p1Food;
	private JLabel p2Food;
	private JLabel p3Food;
	private JLabel p4Food;
	private JLabel p1Energy;
	private JLabel p2Energy;
	private JLabel p3Energy;
	private JLabel p4Energy;
	private JLabel p1Smithore;
	private JLabel p2Smithore;
	private JLabel p3Smithore;
	private JLabel p4Smithore;
	private JLabel food;
	private JLabel energy;
	private JLabel smithore;
	
	private JPanel[] colorPanels;
	private JLabel[] foodLabels;
	private JLabel[] energyLabels;
	private JLabel[] smithoreLabels;
	
	public ProductionPanel() {
		GridLayout layout = new GridLayout(4, 5);
		layout.setHgap(10);
		setLayout(layout);
		setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		
		colorPanels = new JPanel[4];
		foodLabels = new JLabel[4];
		energyLabels = new JLabel[4];
		smithoreLabels = new JLabel[4];
				
		add(new JLabel()); //spacer at top left corner
		
		p1Color = new JPanel();
		add(p1Color);
		colorPanels[0] = p1Color;
		
		p2Color = new JPanel();
		add(p2Color);
		colorPanels[1] = p2Color;
		
		p3Color = new JPanel();
		add(p3Color);
		colorPanels[2] = p3Color;
		
		p4Color = new JPanel();
		add(p4Color);
		colorPanels[3] = p4Color;
		
		//----------------------------------------------------------
		
		food = new JLabel();
		food.setIcon(new ImageIcon("Images\\icon_food.png"));
		add(food);
		
		p1Food = new JLabel();
		add(p1Food);
		foodLabels[0] = p1Food;
		
		p2Food = new JLabel();
		add(p2Food);
		foodLabels[1] = p2Food;
		
		p3Food = new JLabel();
		add(p3Food);
		foodLabels[2] = p3Food;
		
		p4Food = new JLabel();
		add(p4Food);
		foodLabels[3] = p4Food;

		//----------------------------------------------------------
		
		energy = new JLabel();
		energy.setIcon(new ImageIcon("Images\\icon_energy.png"));
		add(energy);
		
		p1Energy = new JLabel();
		add(p1Energy);
		energyLabels[0] = p1Energy;
		
		p2Energy = new JLabel();
		add(p2Energy);
		energyLabels[1] = p2Energy;
		
		p3Energy = new JLabel();
		add(p3Energy);
		energyLabels[2] = p3Energy;
		
		p4Energy = new JLabel();
		add(p4Energy);
		energyLabels[3] = p4Energy;

		//----------------------------------------------------------
		
		smithore = new JLabel();
		smithore.setIcon(new ImageIcon("Images\\icon_smithore.png"));
		add(smithore);
		
		p1Smithore = new JLabel();
		add(p1Smithore);
		smithoreLabels[0] = p1Smithore;
		
		p2Smithore = new JLabel();
		add(p2Smithore);
		smithoreLabels[1] = p2Smithore;
		
		p3Smithore = new JLabel();
		add(p3Smithore);
		smithoreLabels[2] = p3Smithore;
		
		p4Smithore = new JLabel();
		add(p4Smithore);
		smithoreLabels[3] = p4Smithore;
		
		for(JLabel label : foodLabels) {
			label.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
			label.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		}
		
		for(JLabel label : energyLabels) {
			label.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
			label.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		}
		
		for(JLabel label : smithoreLabels) {
			label.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
			label.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		}
	}
	
	/**
	 * 
	 * @param production The amount of production for the turn
	 * @param playerNumber The current player number
	 */
	public void updateProductionPanel(int[] production, int playerNumber) {
		foodLabels[playerNumber].setText("" + production[0]);
		energyLabels[playerNumber].setText("" + production[1]);
		smithoreLabels[playerNumber].setText("" + production[2]);
	}
	
	/**
	 * 
	 * @param players The players in the game
	 */
	public void colorProductionPanel(Player[] players) {
		for(int i = 0; i < players.length; i++) {
			colorPanels[i].setBackground(players[i].getPlayerColor());
		}
	}
}
